<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="${package}.dao.${daoClassName}">


	<!-- ${table.tableComment}${table.tableName}表全字段映射关系 -->
	<resultMap id="BaseResultMap" type="${package}.entity.${entityClassName}">
		<#list table.tableColumnList as column>
		<#if column.isPrimaryKey == true>
		<id column="${table.primaryKey.columnName}" property="${table.primaryKey.columnNameHump}" jdbcType="${table.primaryKey.jdbcType}" />
		</#if>
		</#list>
		<#list table.tableColumnList as column>
		<#if column.isPrimaryKey == false>
		<result column="${column.columnName}" property="${column.columnNameHump}" jdbcType="${column.jdbcType}" />
		</#if>
		</#list>
	</resultMap>
	
	
	<!-- ${table.tableComment}${table.tableName}表全字段 -->
	<sql id="Base_Column_List">
	<#list table.tableColumnList as column>
		${column.columnName} <#sep>, </#sep>
	</#list>
	</sql>
	
	
	<!-- ${r"#{"}query.xxxx}的条件，用于where语句，多个条件以and连接，收尾都不带and -->
	<sql id="Query_All_Column_Where">
		<trim suffixOverrides="and" prefixOverrides="and">
			<#list table.tableColumnList as column>
			<if test="query.${column.columnNameHump} != null">
				${column.columnName} = ${r"#{query."+column.columnNameHump},jdbcType=${column.jdbcType}} and
			</if>
			</#list>
		</trim>			
	</sql>
	
	
<#if config.daoJavaConfig.isCreateCountMethod == true>
	<!-- 获取数量 -->
	<select id="${config.daoJavaConfig.countMethodName}" parameterType="${package}.vo.${voClassName}" resultType="int">
		SELECT 
			count(1) as num
		FROM
			${table.tableName}
		<where>
			<trim suffixOverrides="and">
				<#list table.tableColumnList as column>
				<if test="${column.columnNameHump} != null">
					${column.columnName} = ${"#{"+column.columnNameHump},jdbcType=${column.jdbcType}} and
				</if>
				</#list>
			</trim>
		</where>
	</select>
</#if>



<#if config.daoJavaConfig.isCreateGetAllPageMethod == true>
	<!-- 条件查全部,分页 -->
	<select id="${config.daoJavaConfig.getAllPageMethodName}" resultMap="BaseResultMap" parameterType="${package}.vo.${voClassName}">
		<bind name= "startOffSet" value="(pageNum - 1) * pageSize + 1"/>
		<bind name= "endOffSet" value="startOffSet + pageSize - 1"/>
		SELECT
			* 
		FROM
			( 
				SELECT ROWNUM ROWNUM_ , ROW_.*
				FROM 
					( 
						SELECT 
							<include refid="Base_Column_List" />
						FROM 
							${table.tableName} 
						<where>
							<include refid="Query_All_Column_Where" />
						</where>
						order by ${table.primaryKey.columnName}
					) ROW_ 
				WHERE ROWNUM &lt;= ${"#{"}endOffSet}
			) 
		WHERE
			ROWNUM_ &gt;= ${"#{"}startOffSet}
		
	</select>
</#if>

	
<#if config.daoJavaConfig.isCreateGetAllMethod == true>
	<!-- 条件查全部 -->
	<select id="${config.daoJavaConfig.getAllMethodName}" resultMap="BaseResultMap" parameterType="${package}.vo.${voClassName}">
		SELECT 
			<include refid="Base_Column_List" />
		FROM
			${table.tableName}
		<where>
			<trim suffixOverrides="and">
				<#list table.tableColumnList as column>
				<if test="${column.columnNameHump} != null">
					${column.columnName} = ${"#{"+column.columnNameHump},jdbcType=${column.jdbcType}} and
				</if>
				</#list>
			</trim>
		</where>
		order by ${table.primaryKey.columnName}
	</select>
</#if>
	

<#if config.daoJavaConfig.isCreateGetOneByIdMethod == true>
	<!-- 根据主键查询 -->
	<select id="${config.daoJavaConfig.getOneByIdMethodName}" resultMap="BaseResultMap" parameterType="${table.primaryKey.javaClassName}">
		SELECT 
			<include refid="Base_Column_List" />
		FROM
			${table.tableName}
		WHERE
			${table.primaryKey.columnName} = ${"#{"}id,jdbcType=${table.primaryKey.jdbcType}}
	</select>
</#if>


<#if config.daoJavaConfig.isCreateDeleteByIdMethod == true>
	<!-- 根据主键删除 -->
	<delete id="${config.daoJavaConfig.deleteByIdMethodName}" parameterType="${table.primaryKey.javaClassName}">
		delete from ${table.tableName}
		where ${table.primaryKey.columnName} = ${"#{"}id,jdbcType=${table.primaryKey.jdbcType}}
	</delete>
</#if>
	

<#if config.daoJavaConfig.isCreateInsertNotNullFieldMethod == true>
	<!-- 非空字段新增 -->
	<insert id="${config.daoJavaConfig.insertNotNullFieldMethodName}" parameterType="${package}.entity.${entityClassName}">
		insert into ${table.tableName}
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<#list table.tableColumnList as column>
			<if test="${column.columnNameHump} != null">
				${column.columnName},
			</if>
			</#list>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			<#list table.tableColumnList as column>
			<if test="${column.columnNameHump} != null">
				${"#{"+column.columnNameHump},jdbcType=${column.jdbcType}},
			</if>
			</#list>
		</trim>
	</insert>
</#if>


<#if config.daoJavaConfig.isCreateInsertAllFieldMethod == true>
	<!-- 全字段新增 -->
	<insert id="${config.daoJavaConfig.insertAllFieldMethodName}" parameterType="${package}.entity.${entityClassName}">
		insert into ${table.tableName}
		(
			<#list table.tableColumnList as column>
			${column.columnName} <#sep>, </#sep>
			</#list>
		)
		values (
			<#list table.tableColumnList as column>
			${"#{"+column.columnNameHump},jdbcType=${column.jdbcType}} <#sep>, </#sep>
			</#list>
		)
	</insert>
</#if>
	

	
<#if config.daoJavaConfig.isCreateUpdateNotNullFieldMethod == true>
	<!-- 非空字段更新 -->
	<update id="${config.daoJavaConfig.updateNotNullFieldMethodName}" parameterType="${package}.entity.${entityClassName}">
		update ${table.tableName}
		<set>
			<trim suffixOverrides=",">
				<#list table.tableColumnList as column>
					<#if column.isPrimaryKey != true>
	   			<if test="${column.columnNameHump} != null">
					${column.columnName} = ${"#{"+column.columnNameHump},jdbcType=${column.jdbcType}},
				</if>
			   		</#if>
				</#list>
			</trim>
		</set>
		where ${table.primaryKey.columnName} = ${"#{"}id,jdbcType=${table.primaryKey.jdbcType}}
	</update>
</#if>


<#if config.daoJavaConfig.isCreateUpdateAllFieldMethod == true>
	<!-- 全字段更新 -->
	<update id="${config.daoJavaConfig.updateAllFieldMethodName}" parameterType="${package}.entity.${entityClassName}">
		update ${table.tableName}
		<set>
			<trim suffixOverrides=",">
				<#list table.tableColumnList as column>
					<#if column.isPrimaryKey != true>
	   			${column.columnName} = ${"#{"+column.columnNameHump},jdbcType=${column.jdbcType}},
			   		</#if>
				</#list>
			</trim>
		</set>
		where ${table.primaryKey.columnName} = ${"#{"}id,jdbcType=${table.primaryKey.jdbcType}}
	</update>
</#if>

</mapper>